package com.xs.ahoi.hcxs.utils;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * TTS文本分割
 *
 * @author: fuhuichao
 * @date: 2022/5/10 4:13 下午
 */
public class StringSplitUtils {

    /**
     * 分割字符串
     */
    public static List<String> split(String text) {
        String text1 = filterEmoji(text, " ");
        List<String> lines = new ArrayList<>();
        String[] sArr = separatedText(text1, "[:：。！？!?]").toArray(new String[]{});
        for (int i = 0; i < sArr.length; ++i) {
            //检查语句是否过长
            String s = sArr[i];
            if (s.getBytes().length > 150) {
                //按照逗号和分号分割
                String[] s2Arr = separatedText(s, "[，；,;]").toArray(new String[]{});
                for (int j = 0; j < s2Arr.length; j++) {
                    for (int k = 0; k < (int) (Math.ceil(s2Arr[j].length() / 80f)); ++k) {
                        String str = s2Arr[j].substring(k * 80, Math.min(s2Arr[j].length(), (k + 1) * 80));
                        if (!isEmpty(str) && !isPunct(str)) {
                            lines.add(str);
                        }
                    }
                }
            } else {
                if (!isEmpty(s) && !isPunct(s)) {
                    lines.add(s);
                }
            }
        }
        return lines;
    }

    private static boolean isPunct(String str) { //检测是否全为标点

        Pattern pattern4 = Pattern.compile("\\p{Punct}+");
        Matcher matcher4 = pattern4.matcher(str);
        if (matcher4.matches()) {
            return true;
        }
        return false;
    }

    private static boolean isEmpty(String... strings) {
        for (String str : strings) {
            if (str == null || str.trim().length() == 0)
                return true;
        }
        return false;
    }

    private static List<String> separatedText(String text, String split) {
        List<String> lines = new ArrayList<>();
        int loc = 0;
        for (int i = 0; i < text.length(); i++) {
            String iText = text.substring(i, i + 1);
            if (split.contains(iText)) {
                String subString = text.substring(loc, i + 1);
                Pattern p = Pattern.compile("[\u4e00-\u9fa5]|[A-Z]|[a-z]|[0-9]");
                Matcher m = p.matcher(subString);
                if (m.find()) {
                    lines.add(subString);
                    loc = i + 1;
                }
            }
            if (i + 1 == text.length() && split.contains(iText) == false) {//判断最后一个字符是否包含分隔符,如果不包含把最后一句添加到数组
                String subString = text.substring(loc, i + 1);
                Pattern p = Pattern.compile("[\u4e00-\u9fa5]|[A-Z]|[a-z]|[0-9]");
                Matcher m = p.matcher(subString);
                if (!m.find()) {//无效的文本的处理
                    if (lines.size() != 0) {//取前一个有效的文本，进行拼接
                        String lastString = lines.get(lines.size() - 1);
                        lastString = lastString + subString;
                        lines.set(lines.size() - 1, lastString);
                    } else {
                        lines.add(subString);
                    }
                } else {
                    lines.add(subString);
                }
            }
        }
        return lines;
    }


    /**
     * emoji表情替换
     *
     * @param source  原字符串
     * @param slipStr emoji表情替换成的字符串
     * @return 过滤后的字符串
     */
    private static String filterEmoji(String source, String slipStr) {
        if (!TextUtils.isEmpty(source)) {
            return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", slipStr);
        } else {
            return source;
        }
    }

}
